//
//  HWFirstTimeGuidanceViewController.m
//  HuaiWu
//
//  Created by HuaiWu on 2022.
//  Copyright ©2022年 HuaiWu. All rights reserved.
//

#import "HWFirstTimeGuidanceViewController.h"

@interface HWFirstTimeGuidanceViewController () <UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *myButtons;
@property (assign, nonatomic) NSInteger index;
@property (weak, nonatomic) IBOutlet UIButton *myButton;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottonBottom;

@end

@implementation HWFirstTimeGuidanceViewController

#pragma mark - Life Cycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = UIColorFromHex(0x007CFD);
    self.navigationBar.hidden = YES;
    _bottonBottom.constant = 49;
    _myButton.adjustsImageWhenHighlighted = NO;
    [_myButton setTitle:@"继续" forState:UIControlStateNormal];
    [self setupSubViews];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

- (void)setupSubViews
{
    NSInteger pageCount = 3;
    for (NSInteger i = 0 ; i < pageCount; i++) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(i * kScreenWidth, -self.statusBarHeight, kScreenWidth, kScreenHeight)];
                        
        NSString *str = [NSString stringWithFormat:@"yd_0%ld",(long)i + 1];
        UIImage *image = [UIImage imageNamed:str];

        float width = kScreenWidth;
        float height = width * image.size.height / image.size.width;

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, width, height)];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.clipsToBounds = true;
        imageView.image = image;

        [view addSubview:imageView];

        [self.scrollView addSubview:view];
    }
    self.scrollView.contentSize = CGSizeMake(kScreenWidth * pageCount + 50, 0);
}
    
#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    _index = scrollView.contentOffset.x / kScreenWidth;
    
    if (scrollView.contentOffset.x > kScreenWidth*2) {
        [self startBtnClick];
    } else {
        NSInteger page = scrollView.contentOffset.x / kScreenWidth;
        
        [_myButtons enumerateObjectsUsingBlock:^(UIButton *button, NSUInteger idx, BOOL * _Nonnull stop) {
            if (button.tag - 10 == page) {
                button.alpha = 1.0;
            } else {
                button.alpha = 0.45;
            }
        }];
    }
}

- (IBAction)buttonAction:(UIButton *)sender {
    
    if (_index >= 2) {
        [self startBtnClick];
    } else {
        _index ++;
        [self.scrollView setContentOffset:CGPointMake(kScreenWidth * _index,               self.scrollView.contentOffset.y) animated:YES];
        
        [_myButtons enumerateObjectsUsingBlock:^(UIButton *button, NSUInteger idx, BOOL * _Nonnull stop) {
            if (button.tag == _index + 10) {
                button.alpha = 1.0;
            } else {
                button.alpha = 0.45;
            }
        }];
    }
}

- (void)startBtnClick {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:@"YES" forKey:@"isGuide"];
    [userDefaults setObject:@"YES" forKey:@"isFirst"];
    [userDefaults synchronize];
        
    [kApplicationManager setupWindow:kAppDelegate.window];
}

@end
